# -*- encoding:utf-8 -*-
from django import forms
from django.forms import ModelForm, fields, widgets

from bugs.models import Bug, Demand


class AddBugForm(ModelForm):
    class Meta:
        model = Bug

        fields = [
            'version',
            'emerge_times',
            'severity',
            'level',
            'description',
            'assignee',
        ]

    def clean(self):
        print('clean')
        return super(AddBugForm, self).clean()
        cleaned_data = self.cleaned_data
        state = cleaned_data.get('state')
        if state is None:
            msg = "Must put 'state' in bug."
            self.add_error('state', msg)
            raise forms.ValidationError("You have failed validation!")

        level = cleaned_data.get("level")
        print('level')
        if level is None:
            print(2)
            msg = "Must put 'level' in bug."
            self.add_error('level', msg)
            raise forms.ValidationError("You have failed validation!")


class UserForm(forms.Form):
    username = forms.CharField(label="用户名", max_length=128)
    password = forms.CharField(label="密码", max_length=256, widget=forms.PasswordInput)


class AddDemandForm(ModelForm):
    file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

    class Meta:
        model = Demand

        fields = [
            'name',
            'require_type',
            'level',
            'description',
            'difficult',
            'file',
            'state',
            'file',
            'assignee',
        ]

    def clean(self):
        return super(AddDemandForm, self).clean()
        cleaned_data = self.cleaned_data
        state = cleaned_data.get('state')
        if state is None:
            msg = "Must put 'state' in bug."
            self.add_error('state', msg)
            raise forms.ValidationError("You have failed validation!")

        level = cleaned_data.get("level")
        if level is None:
            print(2)
            msg = "Must put 'level' in bug."
            self.add_error('level', msg)
            raise forms.ValidationError("You have failed validation!")
